//
//  SwiftUIFlyApp.swift
//  SwiftUIFly
//
//  Created by shen on 2023/10/18.
//

import SwiftUI

@main
struct SwiftUIFlyApp: App {
    let persistenceController = PersistenceController.shared

    // 适配UIAppDelegate
    #if !os(macOS)
    @UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
    #endif

    @Environment(\.scenePhase) private var scenePhase // 最小化再打开看logger

    var body: some Scene {
        WindowGroup {
            // test1 - macos
//            ExampleNavigationSplitView()

//            // test2 - iOS
//            ExampleSampleNavigationStackRestore()

            MainView()
                .environment(\.managedObjectContext, persistenceController.container.viewContext)
        }
        #if os(macOS)
        // 设置窗口默认尺寸
        .defaultSize(width: 600, height: 800)
        // 设置默认位置
//        .defaultPosition(.center)
        // 新增窗口系统默认快捷键为 ⌘+N 这里换成 ⌘+T
        .keyboardShortcut("t", modifiers: [.command])
        #endif
        .onChange(of: scenePhase) { oldValue, newValue in
            logger.info("Scene => \(oldValue) -> \(newValue)") // 最小化再打开看logger
        }
    }
}

#if !os(macOS)
class AppDelegate: NSObject, UIApplicationDelegate {
    func application(_: UIApplication, didFinishLaunchingWithOptions _: [UIApplication.LaunchOptionsKey: Any]? = nil) -> Bool {
        logger.info("!macOS ==> application:didFinishLaunchingWithOptions")
        return true
    }
}
#endif
